package com.example.flower_shop.repository;

import com.example.flower_shop.entity.Product;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long>, JpaSpecificationExecutor<Product> {

    Page<Product> findByCategoryId(Long categoryId, Pageable pageable);

    List<Product> findByNameContainingAndStatus(String name, Integer status);

    @Query("SELECT p FROM Product p WHERE p.stock > 0 AND p.status = 1")
    Page<Product> findAvailableProducts(Pageable pageable);

    @Query("SELECT p FROM Product p WHERE p.category.id = ?1 AND p.stock > 0 AND p.status = 1")
    Page<Product> findAvailableProductsByCategory(Long categoryId, Pageable pageable);

    // 获取轮播图商品
    @Query("SELECT p FROM Product p WHERE p.isCarousel = true AND p.status = 1 ORDER BY p.carouselSort DESC")
    List<Product> findCarouselProducts();

    // 获取推荐商品
    @Query("SELECT p FROM Product p WHERE p.isRecommended = true AND p.status = 1 ORDER BY p.recommendSort DESC")
    Page<Product> findRecommendedProducts(Pageable pageable);

    // 获取热销商品
    @Query("SELECT p FROM Product p WHERE p.status = 1 ORDER BY p.salesCount DESC")
    Page<Product> findHotProducts(Pageable pageable);

    Page<Product> findByCategoryIdAndStatus(Long categoryId, Integer status, Pageable pageable);

    Page<Product> findByIsCarouselTrueAndStatusOrderByCarouselSortDesc(Integer status, Pageable pageable);

    Page<Product> findByIsRecommendedTrueAndStatusOrderByRecommendSortDesc(Integer status, Pageable pageable);

    Page<Product> findByIsHotTrueAndStatusOrderByIdDesc(Integer status, Pageable pageable);
}